import Vue from 'vue'
import Router from 'vue-router'

/* 防止路由跳转报错 */
const originalPush = Router.prototype.push
Router.prototype.push = function push(location, resolve, reject) {
  if ( resolve || reject ) return originalPush.call(this, location, resolve, reject)
  return originalPush.call(this, location).catch((e)=>{})
}
Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import RouterView from '@/components/RouterView'

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/register',
    component: () => import('@/views/register/index'),
    hidden: true
  },
  {
    path: '/pull/log',
    component: () => import('@/views/system/log/audit/poll'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/500',
    component: () => import('@/views/error-page/500'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard/workplace',
    hidden: true,
    children: [
      {
        path: '/order/donate',
        component: () => import('@/views/mall/pay/donate'),
        name: 'donate',
        hidden: true
      },
      {
        path: '/member/payment',
        component: () => import('@/views/system/member/payment'),
        hidden: true
      },
      {
        path: '/order/confirm',
        component: () => import('@/views/mall/order/confirm'),
        hidden: true
      },
      {
        path: '/training/exam',
        component: () => import('@/views/training/exam'),
        hidden: true
      }
    ]
  }
]

export const staticRoutes = [
  {
    path: '/dashboard',
    component: Layout,
    redirect: '/dashboard/workplace',
    name: 'Dashboard',
    meta: {id: 'dashboard', title: '首页', icon: 'Home'},
    children: [
      {
        path: '/dashboard/workplace',
        component: () => import('@/views/dashboard/workplace'),
        name: '工作台',
        meta: {id: 'workplace', title: '工作台', affix: true}
      },
      {
        path: '/dashboard/analysis',
        component: () => import('@/views/dashboard/analysis'),
        name: '分析页',
        meta: {id: 'analysis', title: '分析页'}
      }
    ]
  }
]

export const staticHeadRoutes = [
  {
    path: '/dashboard',
    component: Layout,
    redirect: '/dashboard/workplace',
    name: 'Dashboard',
    meta: {id: 'dashboard', title: '首页', icon: 'Home'}
  }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
